package springboot.dao;

import springboot.domain.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class PermissionDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public List<Permission> getPermissionsByIds(List<Integer> id){
        List<Permission> permissions = new ArrayList<>();
        for (int oneOfId :id){
            Permission permission = this.getPermissionById(oneOfId);
            permissions.add(permission);
        }
        return permissions;
    }
    public Permission getPermissionById(Integer id) {
        String sql = "select * from permission where id = ?";
        RowMapper<Permission> rowMapper = new BeanPropertyRowMapper<Permission>(Permission.class);
        List<Permission> permissions = jdbcTemplate.query(sql, new Object[]{id},rowMapper);
        if (permissions.size()>0) {
            return permissions.get(0);
        } else {
            return null;
        }
    }
}
